package mvc.command.armory
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;

	import define.CharacterConst;

	import mvc.control.ArmoryEvent;

	import mx.collections.ArrayCollection;
	import mx.collections.Sort;
	import mx.core.Application;

	import nz.co.codec.flexorm.EntityManager;
	import nz.co.codec.flexorm.criteria.Criteria;

	import orm.fantasy.TblCharacterVO;
	import orm.fantasy.TblItemVO;

	import view.design.DesignSlot;

	public class SearchOptionalItemCmd implements ICommand
	{
		private var _event:ArmoryEvent;
		private var _em:EntityManager;

		public function execute(event:CairngormEvent):void
		{
			_event = event as ArmoryEvent;
			_em = EntityManager.instance;

			var tblItemList:ArrayCollection = doSearch();
			Application.application.dsnMain.dsnSelector.showOptional(tblItemList);
		}
		public static var dsnSlot:DesignSlot;

		private function doSearch():ArrayCollection
		{
			dsnSlot = _event.dsnSlot;
			var tblCharacterVO:TblCharacterVO = dsnSlot.tblCharacterVO;
			var availableSlots:Array = dsnSlot.availableSlots;

			var tblItemList:ArrayCollection = new ArrayCollection();
			for each(var availableSlot:String in availableSlots)
			{
				var criteria:Criteria = _em.createCriteria(TblItemVO);
				criteria.addEqualsCondition("is_token", false);
				criteria.addEqualsCondition("slot", availableSlot);
				if(tblCharacterVO.prop_class == CharacterConst.DEATH_KNIGHT)
					criteria.addEqualsCondition("fit_death_knight", true);
				else if(tblCharacterVO.prop_class == CharacterConst.DRUID)
					criteria.addEqualsCondition("fit_druid", true);
				else if(tblCharacterVO.prop_class == CharacterConst.HUNTER)
					criteria.addEqualsCondition("fit_hunter", true);
				else if(tblCharacterVO.prop_class == CharacterConst.MAGE)
					criteria.addEqualsCondition("fit_mage", true);
				else if(tblCharacterVO.prop_class == CharacterConst.PALADIN)
					criteria.addEqualsCondition("fit_paladin", true);
				else if(tblCharacterVO.prop_class == CharacterConst.PRIEST)
					criteria.addEqualsCondition("fit_priest", true);
				else if(tblCharacterVO.prop_class == CharacterConst.ROGUE)
					criteria.addEqualsCondition("fit_rogue", true);
				else if(tblCharacterVO.prop_class == CharacterConst.SHAMAN)
					criteria.addEqualsCondition("fit_shaman", true);
				else if(tblCharacterVO.prop_class == CharacterConst.WARLOCK)
					criteria.addEqualsCondition("fit_warlock", true);
				else if(tblCharacterVO.prop_class == CharacterConst.WARRIOR)
					criteria.addEqualsCondition("fit_warrior", true);

				if(tblCharacterVO.prop_type == CharacterConst.TYPE_CASTER)
					criteria.addEqualsCondition("fit_caster", true);
				else if(tblCharacterVO.prop_type == CharacterConst.TYPE_HEALER)
					criteria.addEqualsCondition("fit_healer", true);
				else if(tblCharacterVO.prop_type == CharacterConst.TYPE_MELEE)
					criteria.addEqualsCondition("fit_melee", true);
				else if(tblCharacterVO.prop_type == CharacterConst.TYPE_TANK)
					criteria.addEqualsCondition("fit_tank", true);
				tblItemList.addAll(_em.fetchCriteria(criteria));
			}

			return tblItemList;
		}

	}
}